<?php
/**
 * 奖励赠送：新潮浏览器、添加免费正版下载、添加收费正版下载
 */

class BonusModel
{
    public function __construct()
    {
    }

    public function getOs($ua)
    {
        $ua = strtolower($ua);
        $name = 'other';
        if ((stripos($ua, 'Windows NT 5.1') !== false) || (strpos($ua, 'Windows NT 5.2') !== false)) {
            $name = 'xp';
        } elseif(stripos($ua, 'Windows NT 6.0') !== false) {
            $name = 'vista';
        } elseif(stripos($ua, 'Windows NT 6.1') !== false) {
            $name = '7';
        } elseif(stripos($ua, 'Windows NT 6.2') !== false) {
            $name = '8';
        }

        $bits = 32;
        if ((stripos($ua, 'Win64') !== false) || (stripos($ua, 'x64') !== false) || (stripos($ua, 'WOW64') !== false) || (stripos($ua, 'x86_64') !== false)) {
            $bits = 64;
        }
        return array(
            //'brand' => '',
            'name' => $name,
            'bits' => $bits,
        );
    }

    public function getApp($ua)
    {
        $ua = strtolower($ua);
        $name = 'other';
        $version = '0';
        if (strpos($ua, 'lbbrowser') !== false) {
            $name = 'lbbrowser';
            $version = isset($data['version']) ? $data['version'] : '1.0';
        } elseif(strpos($ua, 'chrome') !== false) {
            $name = 'chrome';
            $pos = strpos($ua, 'chrome');
            $tmp = substr($ua, $pos); //Chrome/23.0.1271.64 Safari/537.11
            $tmp1 = explode(' ', $tmp); //为什么用explode而不用截取？是考虑到万一Chrome/23.0.1271.64后面没有东西了。
            $tmp2 = explode('/', $tmp1[0]);
            $version = $tmp2[1];
        } elseif(strpos($ua, 'firefox') !== false) {
            $name = 'firefox';
            $pos = strpos($ua, 'firefox');
            $tmp = substr($ua, $pos);
            $tmp1 = explode(' ', $tmp);
            $tmp2 = explode('/', $tmp1[0]);
            $version = $tmp2[1];
        } elseif(strpos($ua, 'msie') !== false) {
            $name = 'ie';
            $pos = strpos($ua, 'msie'); //Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0)
            $tmp = substr($ua, $pos);
            $tmp1 = explode(';', $tmp);
            $tmp2 = explode(' ', $tmp1[0]);
            $version = $tmp2[1];
        }
        return array(
            'name' => $name,
            'version' => $version,
        );
    }

    /**
     * 检查今天是否已登录
     */
    public function isTodayLogined($user_id)
    {
        $log_db = new LogDb();
        $now = time();
        $data = array(
            'user_id' => $user_id,
            'day' => gmdate('Ymd', $now + (8 * 3600)), //服务器是UTC，本站认为用户都是+8区
            'action' => LogDb::$action['web_login'],
        );
        $where = array(
            'e' => $data,
        );
        $r = $log_db->selectRow($where);
        if (!empty($r)) {
            return true;
        }
        return false;
    }

    /**
     * 网站登录奖励：浏览器、os
     */
    public function setWebLoginBonus($user_id, $ua, $data)
    {
        $user_agent_score = intval($data['score']);

        if ($this->isTodayLogined($user_id)) {
            return true;
        }
        $log_db = new LogDb();
        $now = time();
        $data = array(
            'user_id' => $user_id,
            'day' => gmdate('Ymd', $now + (8 * 3600)),
            'action' => LogDb::$action['web_login'],
        );
        $new_log = $data;
        $new_log['time'] = $now;
        $r = $log_db->insertRow($new_log); //todo exception

        if($user_agent_score > 1000) { //防作弊，这样太简陋。如果以后浏览器超过1000分怎么办？
            $user_agent_score = 100;
        }
        $msg = '你的浏览器获得' . $user_agent_score . '分，' . '赠送' . $user_agent_score . 'MB流量';
        $bonus = $user_agent_score * 1024 * 1024;
        $this->addBonus($user_id, $bonus, 'browser_bonus', $msg);

        $os = $this->getOs($ua);
        $msg = '使用' . $os['bits'] . '位系统，' . '赠送' . $os['bits'] . 'MB流量';
        $bonus = $os['bits'] * 1024 * 1024;
        $this->addBonus($user_id, $bonus, 'os_bonus', $msg);
        return true;
    }

    /**
     * 添加奖励、发送提醒
     */
    private function addBonus($user_id, $bonus, $action, $msg)
    {
        $now = time();
        $user_db = new UserDb();
        $where = array(
            'e' => array(
                'id' => $user_id,
            ),
        );
        $r = $user_db->selectRow($where, array('uploaded'));
        $data = array(
            'uploaded' => $r['uploaded'] + $bonus,
        );
        $r = $user_db->update($data, $where);

        //发通知
        $notification_db = new NotificationDb();
        $notification = array(
            'user_id' => $user_id,
            'action' => NotificationDb::$action[$action],
            'create_time' => $now,
            'content' => $msg,
        );
        $r = $notification_db->insertRow($notification); //todo exception
        return true;
    }
}
?>
